/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rallyracer;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author samra_000
 */
public class RallyManager 
{
    private TreeMap<Integer, Segment> segments;
    
    public RallyManager()
    {
        this.segments = Maps.newTreeMap();
    }
    
    public void addSegment( Segment segment ) throws DuplicateSegmentInRaceException
    {
        if(this.segments.containsKey(segment.getSegmentNumber()))
        {
            throw new DuplicateSegmentInRaceException("Trying to Create Duplicate Segment", this, segment);
        }
        this.segments.put(segment.getSegmentNumber(), segment);
    }
    
    public void parseRally(String location) throws FileNotFoundException, IOException
    {
        File dir = new File(location);
        System.out.println( "location: " + location + "path: " + dir.getAbsolutePath() + " is dir: " + dir.isDirectory() );
        File[] files = dir.listFiles();
        File rallyDir = null;
        for( File f: files )
        {
            System.out.println("Name of dir: " + f.getName() );
            if( f.getName().contains("rally123") )
            {
              rallyDir = f;
              System.out.println( "rally dir: " + f.getAbsolutePath() );
              break;
            }
        }
        files = rallyDir.listFiles();
        List<File> csvFiles = Lists.newArrayList();
        for( File f: files )
        {
            if( !f.getName().contains("csv") )
            {
                continue;
            }
            System.out.println("Recognized file: " + f.getAbsolutePath() + "\n Now Parsing... " );
            csvFiles.add(f);
            Segment curSegment = null;
            try 
            {
                curSegment = SegmentParser.parse(f);
                this.addSegment(curSegment);
                curSegment.compute(curSegment.getAbsoluteStartTime());
                curSegment.save();
                System.out.println("printing computed values\n"+curSegment);
            } 
            catch (DuplicateDataPointInSegmentException ex) 
            {
                System.out.println("Duplicate DataPoint found in file: "+f.getName() + ", exception: " + ex );
            }
            catch (DuplicateSegmentInRaceException ex) 
            {
                System.out.println("Found DuplicateSegment!!! "+f.getAbsolutePath() + ", exception: "+ex);
            }
        }
        
    }
    
    public Segment getSegment( int segmentNumber )
    {
        return this.segments.get(segmentNumber);
    }
}
